package com.gitee.wsl.struct.tree


interface TreeElement<T,E:TreeElement<T,E>> {
    val data: T
    val parent: E?
    val elementId: Any

    val isRoot:Boolean get() = parent == null

    //fun path(): List<E>

    val isLeaf :Boolean

    /**
     * Distance is the number of edges along the shortest path between two nodes.
     *
     * @return The distance between current node and the root.
     */
    val depth: Int
        get() {
            var depth = 0
            var tempParent = parent

            while (tempParent != null) {
                depth++
                tempParent = tempParent.parent
            }
            return depth
        }

}

interface LeafElement<T,E:TreeElement<T,E>>:TreeElement<T,E>{
    override val isLeaf: Boolean
        get() = true
}

interface NodeElement<T,E:TreeElement<T,E>>:TreeElement<T,E>{

    val children: List<E>?

    val size:Int get() = children?.size?:0

    fun isEmpty(): Boolean = size == 0

    override val isLeaf: Boolean
        get() = children == null || children?.isEmpty() == true

    val isBranch:Boolean get() = size > 0
}

